package com.elder.business.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.*;
import com.elder.business.mapper.ElderlyapartmentMapper;
import com.elder.business.mapper.MemberInfoMapper;
import com.elder.common.core.domain.R;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.core.redis.RedisCache;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.LongUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.ElderlyApartmentAnnouncementsMapper;
import com.elder.business.service.IElderlyApartmentAnnouncementsService;

/**
 * 公告信息Service业务层处理
 * 
 * @author elder
 * @date 2023-11-26
 */
@Service
public class ElderlyApartmentAnnouncementsServiceImpl implements IElderlyApartmentAnnouncementsService 
{
    @Autowired
    private ElderlyApartmentAnnouncementsMapper elderlyApartmentAnnouncementsMapper;
    @Autowired
    private MemberInfoServiceImpl memberInfoService;
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private ElderlyapartmentMapper elderlyapartmentMapper;
    @Autowired
    private RedisCache redisCache;


    private static final String ANNOUNCEMENTS_KEY = "apartment_announcements";
    /**
     * 查询公告信息
     * 
     * @param id 公告信息主键
     * @return 公告信息
     */
    @Override
    public JSONObject selectElderlyApartmentAnnouncementsById(Long id)
    {
        String str = redisCache.getCacheMapValue(ANNOUNCEMENTS_KEY, StringNewUtils.long2Str(id));
        if(!StringNewUtils.isEmpty(str)){
            return JSONObject.parseObject(str);
        }
        ElderlyApartmentAnnouncements v = elderlyApartmentAnnouncementsMapper.selectElderlyApartmentAnnouncementsById(id);
        String memberName = memberInfoMapper.getMember(v.getPostedId()).getMemberName();
        //获取公寓信息
        List<Elderlyapartment> apartmentInfo = elderlyapartmentMapper.getAllApartmentInfo();
        Map<Long, String> apartmentMap = apartmentInfo.stream()
                .collect(Collectors.toMap(Elderlyapartment::getApartmentId, Elderlyapartment::getApartmentName));
        JSONObject obj = new JSONObject();
        obj.put("id", StringNewUtils.long2Str(v.getId()));
        obj.put("content", v.getContent());
        obj.put("title", v.getTitle());
        obj.put("apartmentId",StringNewUtils.long2Str(v.getApartmentId()));
        obj.put("apartmentName",apartmentMap.get(v.getApartmentId()));
        obj.put("postedName", memberName);
        obj.put("postDate", DateUtils.dateStr(v.getPostDate()));
        redisCache.setCacheMapValue(ANNOUNCEMENTS_KEY,StringNewUtils.long2Str(id),obj.toJSONString());
        return obj;
    }

    /**
     * 查询公告信息列表
     * @return 公告信息
     */
    @Override
    public TableDataInfo selectElderlyApartmentAnnouncementsList(String title,String postedName, String postDate ,String expiryDate,Integer pageNum,Integer pageSize)
    {
        List<Long> userIdList = null;
        List<MemberInfo> memberList = null;
        if(!StringNewUtils.isEmpty(postedName)){
            memberList = memberInfoMapper.getMemberList(postedName,null);
            if (CollectionUtils.isNotEmpty(memberList)){
                userIdList = memberList.stream().map(MemberInfo::getUserId).collect(Collectors.toList());
            }
        }
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        List<ElderlyApartmentAnnouncements> list = elderlyApartmentAnnouncementsMapper.selectElderlyApartmentAnnouncementsList(title,userIdList, LongUtils.str2Long(postDate),LongUtils.str2Long(expiryDate));
        PageInfo<ElderlyApartmentAnnouncements> pageInfo = new PageInfo<>(list);
        List<ElderlyApartmentAnnouncements> elderApartList = pageInfo.getList();
        List<Long> userIds = elderApartList.stream().map(ElderlyApartmentAnnouncements::getPostedId).collect(Collectors.toList());
        //获取成员信息
        List<MemberInfo> memberUserList = memberInfoMapper.getMemberUserList(userIds);
        //获取公寓信息
        List<Elderlyapartment> apartmentInfo = elderlyapartmentMapper.getAllApartmentInfo();
        Map<Long, String> apartmentMap = apartmentInfo.stream()
                .collect(Collectors.toMap(Elderlyapartment::getApartmentId, Elderlyapartment::getApartmentName));
        Map<Long, String> memberMap = memberUserList.stream().collect(Collectors.toMap(MemberInfo::getUserId, MemberInfo::getMemberName));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("获取成功");
        tableDataInfo.setRows(
            elderApartList.stream().map(v->{
                JSONObject obj = new JSONObject();
                obj.put("id", StringNewUtils.long2Str(v.getId()));
                obj.put("content", v.getContent());
                obj.put("title", v.getTitle());
                obj.put("apartmentId",StringNewUtils.long2Str(v.getApartmentId()));
                obj.put("apartmentName",apartmentMap.get(v.getApartmentId()));
                obj.put("postedName", memberMap.get(v.getPostedId()));
                obj.put("postDate", DateUtils.dateStr(v.getPostDate()));
                return obj;
            }).collect(Collectors.toList()));
        return tableDataInfo;
    }

    /**
     * 新增公告信息
     * 
     * @param elderlyApartmentAnnouncements 公告信息
     * @return 结果
     */
    @Override
    public int insertElderlyApartmentAnnouncements(ElderlyApartmentAnnouncements elderlyApartmentAnnouncements)
    {
        elderlyApartmentAnnouncements.setId(SnowGenerator.getId());
        elderlyApartmentAnnouncements.setPostDate(DateUtils.getLongTime());
        return elderlyApartmentAnnouncementsMapper.insertElderlyApartmentAnnouncements(elderlyApartmentAnnouncements);
    }

    /**
     * 修改公告信息
     * 
     * @param announcementsVO 公告信息
     * @return 结果
     */
    @Override
    public int updateElderlyApartmentAnnouncements(ElderlyApartmentAnnouncementsVO announcementsVO)
    {
        ElderlyApartmentAnnouncements announcements = new ElderlyApartmentAnnouncements();
        BeanUtils.copyProperties(announcementsVO, announcements);
        announcements.setExpiryDate(DateUtils.getLongTime());
        return elderlyApartmentAnnouncementsMapper.updateElderlyApartmentAnnouncements(announcements);
    }

    /**
     * 批量删除公告信息
     * 
     * @param ids 需要删除的公告信息主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentAnnouncementsByIds(Long[] ids)
    {
        return elderlyApartmentAnnouncementsMapper.deleteElderlyApartmentAnnouncementsByIds(ids);
    }

    /**
     * 删除公告信息信息
     * 
     * @param id 公告信息主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentAnnouncementsById(Long id)
    {
        return elderlyApartmentAnnouncementsMapper.deleteElderlyApartmentAnnouncementsById(id);
    }

    @Override
    public TableDataInfo getRecentAnnouncementsContent() {
        List<ElderlyApartmentAnnouncements> elderApartList = elderlyApartmentAnnouncementsMapper.getRecentAnnouncementsContent();
        //获取公寓信息
        List<Elderlyapartment> apartmentInfo = elderlyapartmentMapper.getAllApartmentInfo();
        Map<Long, String> apartmentMap = apartmentInfo.stream()
                .collect(Collectors.toMap(Elderlyapartment::getApartmentId, Elderlyapartment::getApartmentName));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("获取成功");
        tableDataInfo.setRows(
                elderApartList.stream().map(v->{
                    JSONObject obj = new JSONObject();
                    obj.put("id", StringNewUtils.long2Str(v.getId()));
                    obj.put("content", v.getContent());
                    obj.put("title", v.getTitle());
                    obj.put("apartmentId",StringNewUtils.long2Str(v.getApartmentId()));
                    obj.put("apartmentName",apartmentMap.get(v.getApartmentId()));
                    obj.put("postedName", v.getPostedName());
                    obj.put("postDate", DateUtils.dateStr(v.getPostDate()));
                    return obj;
                }).collect(Collectors.toList()));
        return tableDataInfo;
    }
}
